#!/usr/bin/perl -w

#-------------------------------------------------------------------------------#
# @(#) SCRIPT : google_dork_scanner.pl                                          #
#-------------------------------------------------------------------------------#
# @(#) Application		: <application>                                         #
# @(#) Fonction         : <fonction du script>                                  #
# @(#) version          : V0.1                                                  #
# @(#) Auteur           : k_pharos                                              #
# @(#) Date de creation : 01/10/2014                                            #
# @(#) Utilisateur      : All                                                   #
# @(#) Parametres d.entree      : ./ser.pl --query "Votre recherche"            #
# @(#) Retour   		: 0                                                     #
# @(#)Commentaires      : (quel programme appelle ce script, etc...)            #
#-------------------------------------------------------------------------------#
# @(#)Modifications            :                                                #
# @(#)date auteur version      : libelle                                        #
#-------------------------------------------------------------------------------#

#-----------------------------------#
# Initialisation de l'environnement #
#-----------------------------------#
use warnings;
use LWP::UserAgent;
use URI::Escape;
use HTTP::Request;
use strict;
use Getopt::Long;
use Parallel::ForkManager;
use WWW::Mechanize;
use LWP::Simple;
use File::Basename;

#-----------------------------------------#
# Options & autres variables.             #
#-----------------------------------------#
# Defaults
my %opt = (
   ua    => "Mozilla/5.0",
   query => "",
);

# Options from the commandline.
GetOptions( 
   'verbose' => \$opt{'verbose'},
   'help'    => \$opt{'help'   },
   'query=s' => \$opt{'query'  },
   'ua=s'    => \$opt{'ua'     },
);

#------------------------------------------------------------#
# help() Affiche l'aide.                                     #
#------------------------------------------------------------#
sub help {

print <<ENDHELP

$0 Recherche google en ligne de commande.

Usage:   $0 --query "Votre recherche" [OPTIONS]

Options:

   --query string  Search string for dork.
                   Required.  No default.

   --verbose       Show what the script is doing as it goes.
                   Defaults to off.
                
   --safe          Use google's safesearch to filter naughty pictures.
                   Defaults to off.

   --ua string     google doesn't like robots.  This is
                   the user-agent string we spoof.
                   Defaults to "Mozilla/1.0"

   --help          You're looking at it, cowboy.

ENDHELP

}
print q {
*******************************************************************
*    ___              _     __                                    *
*    /   \ ___   _ __ | | __/ _\  ___  __ _  _ __                 * 
*   / /\ // _ \ | '__|| |/ /\ \  / __|/ _` || '_ \                *   
*  / /_//| (_) || |   |   < _\ \| (__| (_| || | | |               *    
* /___,'  \___/ |_|   |_|\_\\__/ \___|\__,_||_| |_|               *
*                                                                 *                                                
* DorkScan Ver. 0.2a                                              *
* Coded by k_pharos                                               *
* Security Research                                               *
* k_pharos pharos@katech.fr                                       *
*                                                                 *
*******************************************************************
};

# Compose our base URL for www.google.com.
$opt{'query'} =~ s/ /+/g;
$opt{'query'} = uri_escape($opt{'query'});

# Validate input and display help if needed.
    &help if ($opt{'help'} || !$opt{'query'});

	for (my $start=0;$start<=100000;$start+=10){

	my $url = 'http://www.google.fr/search' .
   	"?q=$opt{'query'}" .
   	"\&safe=" . ($opt{'safe'} ? "on" : "off");

	#------------------------------------------------------------#
	# Create objects we'll need.                                 #
	#------------------------------------------------------------#

	# LWP for HTTP requests.
	my $ua = new LWP::UserAgent;
	$ua->agent('Mozilla/5.0 (X11; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1');
	$ua->timeout(15);
    
	my $page = 1;
   	$opt{'verbose'} && print "Fetching page " . $page . " of results.\n";
   	$page = $page + 1;
    my $request = HTTP::Request->new(GET => $url . "\&start=$start");
   	my $response = $ua->request($request);
	if($response->code != 200){
		die("\nErreur\n");
	}		
   	my $po =  $response->content;
   	my $header = $response->headers_as_string;

	if($po =~ /<p style="padding-top:.33em">/s){
        print "\nAucun résultat!\n";
    }

	my $a = 0;
	while($po =~ m/<h3 class="r"><a href="(.*?)"/g){
        print $1."\n";
   		$a = $a + 1;
		open (FO, ">>$opt{'query'}");
		print FO $1,"\n";
		close(FO);
}
}
